<template>
  <el-dialog :visible.sync="addVal" title="添加测量组" >
    <el-form ref="formGroup">
      <!-- <el-form-item label="中控点">
        <el-select v-model="selectControl" style="width:100%" placeholder="中控点" width="100%">
          <el-option v-for="item in controlList" :key="item.id" :label="item.name" :value="item.id"/>
        </el-select>
      </el-form-item> -->
      <el-form-item label="测距点">
        <el-select v-model="selectBase" style="width:100%" placeholder="请先选择测距点" width="100%">
          <el-option v-for="item in baseList" :key="item.id" :label="'' + item.name + ' (' + item.alias + ') ' + ' [' + item.did + '] '" :name="''" :value="item.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="固定点">
        <el-select v-model="selectCheck" :disabled="selectBase == null" style="width:100%" placeholder="请先选择固定点">
          <el-option v-for="item in checkList" :key="item.id" :label="'' + item.name + ' (' + item.alias + ') ' + ' [' + item.did + '] '" :name="''" :value="item.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="下发指令时间（秒）">
        <el-select v-model="levelId" :disabled="selectBase == null" style="width:100%" placeholder="选择指令下发等级">
          <el-option v-for="item in issuedList" :key="item.id" :label="item.levelName + '~' + item.dinterval + 's'" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="off1">
        <el-input-number v-model="off1"/>
      </el-form-item>
      <el-form-item label="off2">
        <el-input-number v-model="off2"/>
      </el-form-item>
      <el-form-item label="off3">
        <el-input-number v-model="off3"/>
      </el-form-item>
      <el-form-item label="disoff">
        <el-input-number v-model="disoff"/>
      </el-form-item>
      <el-form-item label="discov">
        <el-input-number v-model="discov"/>
      </el-form-item>
      <el-form-item label="通道数量">
        <el-select v-model="channelSum" placeholder="请选择" @change="channelNum = ''">
          <el-option label="二通道" value="2" />
          <el-option label="三通道" value="3" />
        </el-select>
      </el-form-item>
      <el-form-item label="通道选择">
        <el-select v-model="channelNum" placeholder="请选择">
          <el-option v-if="channelSum === '2'" label="12" value="12" />
          <el-option v-if="channelSum === '2'" label="13" value="13" />
          <el-option v-if="channelSum === '2'" label="23" value="23" />
          <el-option v-if="channelSum === '3'" label="全通道" value="0" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否防抖">
        <el-select v-model="antiShake" placeholder="请选择">
          <el-option label="开" value="1" />
          <el-option label="关" value="0" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">添加</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script>

import { getBaseList, getCheckList, createMeasureGroup, getControlList } from '@/api/scene'
import { getIssuedList } from '@/api/issued'
export default {
  props: ['scene', 'addShow'],
  data() {
    return {
      // 中控点list
      // controlList: [],
      // 基准点list，
      baseList: [],
      // 测距点list， 需要根据当前选定的基准点从后端动态放回
      checkList: [],
      // 选定的基准点id（单选）
      selectBase: null,
      selectControl: null,
      // 已经选定的测距点id(允许多选)
      selectCheck: [],
      levelId: '',
      off1: 0,
      off2: 0,
      off3: 0,
      disoff: -540452.7291932534,
      discov: 5,
      channelSum: '3',
      channelNum: '0',
      antiShake: '0',
      // 等级列表
      issuedList: []
    }
  },
  computed: {
    addVal: {
      get() {
        return this.addShow
      },
      set() {
        this.$emit('update:addShow', false)
      }
    }
  },
  watch: {
    selectBase: function() {
      // 重置选定
      this.selectCheck = []
      this.getCheckList()
    }
  },
  mounted() {
    getBaseList({ scene: this.scene }).then(res => {
      this.baseList = res.data
    })
    getIssuedList({ sceneId: this.$route.params.id }).then(res => {
      this.issuedList = res.data
    })
    // getControlList({ sceneId: this.scene }).then(res => {
    //   this.controlList = res.data
    // })
  },
  methods: {
    onSubmit() {
      // if (this.selectCheck.length > 0) {
      //   console.log(1111)
      // this.selectCheck.forEach(item => {
      //   // 异步调用创建
      var data = {
        // controId: this.selectControl,
        sceneId: this.scene,
        status: 0,
        receId: this.selectCheck,
        tranId: this.selectBase,
        minCount: 200,
        levelId: this.levelId,
        off1: this.off1,
        off2: this.off2,
        off3: this.off3,
        disoff: this.disoff,
        discov: this.discov,
        channelSum: Number(this.channelSum),
        channelNum: Number(this.channelNum),
        antiShake: Number(this.antiShake)
      }
      createMeasureGroup(data).then(res => {
        console.log(res)
        this.$emit('refresh', true)
        this.$emit('update:addShow', false)
        this.$refs['formGroup'].resetFields()
      })
      // })
      // }
    },
    getCheckList() {
      getCheckList({ scene: this.scene, tran: this.selectBase }).then(res => {
        console.debug('获取测距点')
        this.checkList = res.data
      })
    }
  }
}
</script>
